package com.chinasoft.mall.activity;

import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.GetGameBean;
import com.chinasoft.mall.model.response.GetGameBean.Data;
import com.chinasoft.mall.model.response.GoodsByClassBean;
import com.chinasoft.mall.util.DateUtil;
import com.chinasoft.mall.util.ShakeInterface;
import com.chinasoft.mall.util.ShakeInterface.OnShakeListener;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NetImageView;

/**
 * 商品详情
 * 
 * @author Administrator
 */
@SuppressLint("HandlerLeak")
// animationDrawable.start(); 开始这个动画
// animationDrawable.stop(); 结束这个动画
// animationDrawable.setAlpha(100);设置动画的透明度, 取值范围(0 - 255)
// animationDrawable.setOneShot(true); 设置单次播放
// animationDrawable.setOneShot(false); 设置循环播放
// animationDrawable.isRunning(); 判断动画是否正在播放
// animationDrawable.getNumberOfFrames(); 得到动画的帧数。
@SuppressWarnings("unchecked")
public class OrderGoodsActivity extends BaseActivity implements
		OnShakeListener, OnClickListener, Runnable {

	ImageView ordergoods_bg;
	GetGameBean.Data data;
	// 根据商品ID获取商品详细信息
	private BaseBean goodsbyid_bb;

	private BaseBean insert_bb;
	private GoodsByClassBean gbcb;
	private NetImageView spimg;
	private TextView spid;
	private TextView sptitle;
	private TextView newprice;
	private TextView oldprice;
	private TextView leavetime;
	private TextView leaveid;
	private Button close;
	private ImageView call;
	private RelativeLayout title_rl;

	private ShakeInterface si;
	private String endTime;
	// 0 时 1 分 2 秒
	private byte time_type;
	private int time = -1;
	private int[] resId={R.drawable.firworks_1,R.drawable.firworks_2,R.drawable.firworks_3,R.drawable.firworks_4};

//	AnimationDrawable ordergoods_ad;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 0) {
				leavetime.setText(time + "");
				if (time_type == 0) {
					leaveid.setText("小时");
				} else if (time_type == 1) {
					leaveid.setText("分");
				} else if (time_type == 2) {
					leaveid.setText("秒");
				}
			} else if(msg.what == 1){
				Toast.makeText(OrderGoodsActivity.this, "十分遗憾，该活动已经结束，谢谢您的关注",
						Toast.LENGTH_LONG).show();
				OrderGoodsActivity.this.finish();
			}else if(msg.what == 2){
				ordergoods_bg.setVisibility(View.VISIBLE);
				if(msg.arg1>=resId.length){
					ordergoods_bg.setVisibility(View.GONE);
				}else{
					ordergoods_bg.setBackgroundResource(resId[msg.arg1]);
					handler.sendMessageDelayed(handler.obtainMessage(2, msg.arg1+1, 0), 150);
				}
			}
		}

	};
	private boolean isCancle = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_goods);
		initView();
		init();
	}

	private void initView() {
		title_rl = (RelativeLayout) findViewById(R.id.title_rl);
		ordergoods_bg = (ImageView) findViewById(R.id.ordergoods_bg);
		spimg = (NetImageView) findViewById(R.id.spimg);
		spid = (TextView) findViewById(R.id.spid);
		sptitle = (TextView) findViewById(R.id.sptitle);
		newprice = (TextView) findViewById(R.id.newprice);
		oldprice = (TextView) findViewById(R.id.oldprice);
		leavetime = (TextView) findViewById(R.id.leavetime);
		leaveid = (TextView) findViewById(R.id.leaveid);
		close = (Button) findViewById(R.id.close);
		call = (ImageView) findViewById(R.id.call);
		close.setOnClickListener(this);
		call.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close:
			this.finish();
			break;
		case R.id.call:
			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initRequest() {
		if (data != null) {
			goodsbyid_bb = new BaseBean(Config.GET_GOODSBYID);
			goodsbyid_bb.reqData.put("goods_id", data.getGoodsId());
			control.sendMessage(goodsbyid_bb);
		}
	}

	private void init() {
		if (sz != null) {
			data = (Data) sz;
			endTime = data.getEndDate();
			// 初始化时间
			initDate();
			new Thread(this).start();
		} else {
			Toast.makeText(this, "数据获取失败", Toast.LENGTH_LONG).show();
			this.finish();
		}
		si = new ShakeInterface(this);
		si.registerOnShakeListener(this);
		si.start();
	}

	private void initDate() {
		if ((time = DateUtil.fieldDifferenceNow(endTime, Calendar.HOUR)) > 1) {
			time_type = 0;
		} else if ((time = DateUtil
				.fieldDifferenceNow(endTime, Calendar.MINUTE)) > 1) {
			time_type = 1;
		} else if ((time = DateUtil
				.fieldDifferenceNow(endTime, Calendar.SECOND)) >= 1) {
			time_type = 2;
		} else {
			time_type = 3;
		}
	}

	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(goodsbyid_bb)) {
			if (goodsbyid_bb.getResData() != null) {
				List<GoodsByClassBean> gbcs = (List<GoodsByClassBean>) goodsbyid_bb
						.getResData().getData();
				if (gbcs != null && gbcs.size() > 0) {
					gbcb = gbcs.get(0);
					spimg.setDownloadurl(StringUtil.goodUrl(gbcb.getGOODS_ID(),gbcb.getTHUM()));
					spid.setText(gbcb.getGOODS_ID());
					sptitle.setText(gbcb.getGOODS_NAME());
					newprice.setText("￥" + gbcb.getPRC());
					oldprice.setText("￥" + gbcb.getMPRC());
				}
			}
		} else if (obj.equals(insert_bb)) {
			call.setImageResource(R.drawable.order_right);
			call.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri
							.parse("tel:" + "4006806278"));
					startActivity(intent);
				}
			});
			title_rl.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri
							.parse("tel:" + "4006806278"));
					startActivity(intent);
				}
			});
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		si.stop();
		isCancle = false;
	}

	@Override
	public void onShake() {
		si.stop();
		if (data == null) {
			Toast.makeText(this, "数据获取失败", Toast.LENGTH_LONG).show();
		} else {
//			ordergoods_ad = (AnimationDrawable) ordergoods_bg.getBackground();
//			ordergoods_ad.setOneShot(true);
//			if (!ordergoods_ad.isRunning()) {
//				ordergoods_ad.start();
//			}
			handler.sendMessageDelayed(handler.obtainMessage(2, 0, 0), 150);
			insert_bb = new BaseBean(Config.INSERT);
			insert_bb.setUrl_type((byte) 2);
			insert_bb.reqData.put("goodsId", data.getGoodsId());
			control.sendMessage(insert_bb);
		}
	}

	@Override
	public void run() {
		while (isCancle) {
			if (endTime == null) {
				break;
			} else {
				initDate();
				if (time_type != 3) {
					handler.sendEmptyMessage(0);
				} else {
					handler.sendEmptyMessage(1);
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
